﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilLib;
using UtilPublic.ServerLoger;
using ServerBase.DataBaseRedis;
using ServerBase.Protocol;
using ServerBase.Other;
using System.Collections.Generic;
using static UtilPublic.Redis.ServerRedis;
using static ServerGame.Service.Dispatcher;
using UtilPublic.Serialization;
using ServerGame.Service;
using static ServerBase.Config.Conf;
using ServerBase.Service;
using UtilPublic.Mysql;
using static ServerBase.DataBaseRedis.RdsTxt;
using static ServerBase.Service.BaseDispatch;
using ServerBase.Config;
using ServerBase.IniFile;
using ServerPublic.Manager;
using System;
using static ServerBase.IniFile.IniConf;
using ServerGame.GameObject;
using System.Net;
using System.Linq;
using ServerPublic.DataBaseMysql;
using ServerBase.LunarSocket;
using ServerPublic.Data;
using ServerGame.Data;

namespace ServerGame.Manager
{
    /// <summary>
    /// 杂项
    /// </summary>
    public static class LoginMisc
    {
        #region 检查更新玩家数据
        //检查更新玩家数据
        public static void UpdatePlayerData(this Player player)
        {
            loger.Debug($"UpdatePlayerData");


            //遍历邮件附件修正
            foreach (var mail in player.DaMail.SortDictMails.Values)
            {
                foreach (var att in mail.Attachments)
                {
                    if (att.Count <= 0)
                    {
                        att.Count = 1;
                    }
                }
            }
        }

        #endregion

        // 初始化玩家数据(计算出一些不存档的数据)
        public static void InitPlayerData(this Player player)
        {
            player.Hp();

            //检查更新玩家数据
            player.UpdatePlayerData();

            player.InitLoop();

            //刚登陆后 强制检查定时更新
            LoopPlayer.LoopPlayerSecond(player, true);
            LoopPlayer.LoopPlayerSecond10(player, true);

            MailMgr.更新获取全局邮件(player);

            player.LevelSetup();

            //刷新密钥
            player.KeyEncryptedRefresh();

            player.ActionSet();
        }

        // 新建玩家初始化
        public static bool InitNewbieData(this Player player, string name, string gender, string family)
        {
            player.Name = name;
            player.Gender = gender;
            player.Family = family;

            InitPlayerProp(player);
            InitPlayerSkill(player);
            InitPlayerUsage(player);

            return true;
        }
        public static bool InitNewbieData2(this Player player)
        {
            player.Full();

            return true;
        }
        public static void InitPlayerUsage(Player player)
        {
            //foreach (var item in ConfListUsage)
            //{
            //    if (!player.DaCommon.DictUsage.ContainsKey(item))
            //    {
            //        player.DaCommon.DictUsage[item] = "无";
            //    }
            //}
        }
        public static void InitPlayerSkill(Player player)
        {
            //玩家公共
            {
                var 玩家默认技能列表 = EGlobalId.玩家默认技能列表.ValueListString();
                var 玩家默认技能等级 = EGlobalId.玩家默认技能等级.ValueInt();
                foreach (var item in 玩家默认技能列表)
                {
                    player.DaCommon.DictSkill[item] = 玩家默认技能等级;
                }
            }
            //门派
            {
                var conf = player.CfgFamily;
                foreach (var items in conf.Skills)
                {
                    player.DaCommon.DictSkill[items[0]] = items[1].ToInt();
                }
            }
            //调试
            // todo 初始玩家全技能
            if (true)
            {
                foreach (var item in ConfSkill.Values)
                {
                    if (player.DaCommon.DictSkill.Count >= 100)
                    {
                        break;
                    }
                    var Level = Util.Random.Next(1, 511);
                    player.DaCommon.DictSkill[item.Id] = Level;
                }
            }
        }
        public static void InitPlayerProp(Player player)
        {
            var val = 25;
            for (EProp i = EProp.str; i <= EProp.kar; i++)
            {
                player[i] = val;
            }
        }

    }
}